package com.gomtel.gomtelsdk;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.gomtel.smartdevice.api.BleManager;
import com.gomtel.smartdevice.bt.ble.BLEDevice;
import com.gomtel.smartdevice.interfaces.OnConnectListener;
import com.gomtel.smartdevice.interfaces.OnScannerListener;

import java.util.ArrayList;
import java.util.List;

public class ScanActivity extends AppCompatActivity {

    private ListView listview;
    private ArrayAdapter adapter;
    private List<String> list = new ArrayList<>();
    private boolean isConnect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        listview = (ListView) findViewById(R.id.listview);
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!isConnect) {
                    isConnect = true;
                    final int pos = position;
                    BleManager.getInstance().stopScan();
                    ToastUtil.showShort(ScanActivity.this, "正在连接设备");
                    listview.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            connect(list.get(pos));
                        }
                    }, 1000);
                }
            }
        });
    }

    private void connect(final String mac) {

        BleManager.getInstance().connect(mac, new OnConnectListener() {
            @Override
            public void onSuccess() {
                isConnect = false;
                MainActivity.MAC = mac;
                ToastUtil.showShort(ScanActivity.this, "已连接上");
                finish();
            }

            @Override
            public void onFailure(Throwable e) {
                isConnect = false;

                ToastUtil.showShort(ScanActivity.this, "连接失败");

            }

            @Override
            public void onTimeout() {
                isConnect = false;
                ToastUtil.showShort(ScanActivity.this, "连接超时");

            }
        });
    }





    @Override
    protected void onResume() {
        super.onResume();

        if (ContextCompat.checkSelfPermission(ScanActivity.this, Manifest.permission
                .ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(ScanActivity.this, new String[]{Manifest.permission
                    .ACCESS_FINE_LOCATION
                    , Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
        } else {
            scan();
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1000) {
            scan();
        }
    }

    private void scan() {
        BleManager.getInstance().scan(10000, new OnScannerListener() {
            @Override
            public void onComplete() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(BLEDevice bleDevice) {
                if (!list.contains(bleDevice.getMacAddress())) {
                    list.add(bleDevice.getMacAddress());
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

}
